package com.example.figurehecheng.controller.merchant;


import com.example.figurehecheng.common.constant.MessageConstant;
import com.example.figurehecheng.common.exception.AccountExist;
import com.example.figurehecheng.common.result.PageResult;
import com.example.figurehecheng.common.result.Result;
import com.example.figurehecheng.common.utils.AliOssUtil;
import com.example.figurehecheng.domain.dto.FoodDTO;
import com.example.figurehecheng.domain.entity.Food;
import com.example.figurehecheng.service.IFoodService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 宇科工作室
 * @since 2024-10-18
 */
@RestController("merchantFoodController")
@RequestMapping("/merchant/food")
@CrossOrigin
@Api("菜品信息")
@Slf4j
@RequiredArgsConstructor
public class FoodController {
    private final IFoodService foodService;
    @Autowired
    private  AliOssUtil aliOssUtil;

    /**
     * 获取所有菜品信息
     */
    @GetMapping("/getDishAll")
    @ApiOperation("获取所有菜品信息")
    public Result<PageResult> getDishAll(
                                   @RequestParam(value = "name",required = false)String name,
                                   @RequestParam(value = "pageNum")Integer pageNum,
                                   @RequestParam(value = "pageSize")Integer pageSize){
        PageResult dishAll = foodService.getDishAll(name, pageNum, pageSize);
        return Result.success(dishAll);
    }

    /**
     * 修改菜品相关信息
     * @param foodDto
     */
    @PostMapping("/updateDish")
    @ApiOperation("修改菜品信息")
    public Result updateDish(@RequestBody FoodDTO foodDto) {
        foodService.updateDish(foodDto);
       return Result.success();
    }

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public Result<String> upload(@RequestParam("file") MultipartFile file) throws IOException {
        if (file == null || file.isEmpty()) {
            throw new AccountExist(MessageConstant.COUPON_NOT_NULL);
        }
        String name = file.getOriginalFilename();
        String suffix = name.substring(name.lastIndexOf("."));
        String fileName= UUID.randomUUID().toString()+suffix;
        System.out.println(fileName);
        String uploadResult = aliOssUtil.upload(file.getBytes(), fileName);
        return Result.success(uploadResult);
    }
    /**
     * 删除菜品相关信息
     */
    @DeleteMapping("/deleteDish")
    @ApiOperation("删除菜品相关信息")
    public void deleteDish(@RequestBody FoodDTO foodDTO){
        foodService.deleteDish(foodDTO);
    }

    /**
     * 新增菜品
     * @param foodDTO
     */
    @PostMapping("/addDish")
    @ApiOperation("添加菜品")
    public void addDish(@RequestBody FoodDTO foodDTO){
        foodService.addDish(foodDTO);
    }

}
